/*
 * RBFMacMessageTypes.h
 *
 *  Created on: 2023年7月10日
 *      Author: fjx
 */

#ifndef MAC_RBFMACMESSAGETYPES_H_
#define MAC_RBFMACMESSAGETYPES_H_
#include "stdint.h"

#define __packed  __attribute__((__packed__))


/*
 * packet[0]的定义
 */
//#define PACKET_0_INCLUDE     //包含自身



#define RBF_KEY_LENGTH_MAX                        16
#define RBF_NONCE_LENGTH_MAX                      9
#define RBF_AD_LENGTH_MAX                         3


#define RBF_HUB_ID                                0
#define RBF_DEFAULT_ROUTEID                       0xFF   //默认的routID
#define RBF_DEFAULT_DESTID                        0xFF   //默认的destID
#define RBF_DEFAULT_SRCID                         0xFF   //默认的srcID
#define RBF_DEFAULT_PANID                         0xFFFFFF   //默认的panId(3Bytes)
#define RBF_DEFAULT_


typedef enum {
  RBF_RET_STATUS_SUCCESS = 0,
  RBF_RET_STATUS_FAIL = -1,


  RBF_RET_STATUS_LEN_ERROR = -2,                  //数据长度不符合
  RBF_RET_STATUS_CMD_ERROR = -3,                  //不符合当前命令
  RBF_RET_STATUS_ERROR_NOT_SUPPORTED = -4,        //不支持当前操作

}RBF_RET_STATUS;

typedef enum {
  RBF_CMD_NODE_PAGING = 0x01,                     //节点寻呼
  RBF_CMD_GATEWAY_ALLOW_JOIN_NETWORK = 0x02,      //网关允许节点入网
  RBF_CMD_NODE_JOIN_CONFIRM = 0x03,               //节点入网确认
  RBF_CMD_NODE_UPLOAD_EVENT = 0x04,               //节点实时事件上报
  RBF_CMD_NODE_UPLOAD_HEARTBEAT = 0x05,           //节点心跳上报
  RBF_CMD_GATEWAY_ACK_EVENT = 0x06,               //网关应答上报
  RBF_CMD_GATEWAY_ACK_HEARTBEAT = 0x07,           //网关心跳上报
  RBF_CMD_GATEWAY_DOWNLOAD = 0x08,                //网关主动下发
  RBF_CMD_NODE_ACK = 0x09,                        //节点应答
  RBF_CMD_GATEWAY_SYNC_BROADCAST = 0x0A,          //网关同步广播
  RBF_CMD_GATEWAY_NOMAL_BROADCAST = 0x0B,         //网关普通广播
  RBF_CMD_NODE_ROAMING = 0x20,                    //节点漫游
  RBF_CMD_NODE_FILE_TRANSFER_BLOCK = 0x4A,        //文件分块传输
  RBF_CMD_NODE_FILE_CHECK_REQ = 0x4B,             //文件校验请求
}RBF_CMD_TYPE;

/*
 * key的类型
 *
 *
 */
/* 废弃该数据结构，转移到RBFKeyNvm.h中定义
typedef enum {
  RBF_KEY_ID_CODEA = 0,                          //codeA
  RBF_KEY_ID_CODEB,
  RBF_KEY_ID_CODEAB,
  RBF_KEY_ID_SYSTEM_KEY_CODE,
  RBF_KEY_ID_BROADCAST_KEY_CODE,
  RBF_KEY_ID_DEFAULT_KEY1,
  RBF_KEY_ID_DEFAULT_KEY2,



  RBF_KEY_ID_NOKEY,
}RBFKeyIdentifier_t;

//*/

typedef struct{
  uint8_t  pRollungCode[4];     //4Bytes
  uint8_t  frameCtl;            //1Byte
  uint8_t  pSyncClock[2];       //2Bytes
  uint8_t  hsclock;             //1Byte
  uint8_t  pPanId[3];           //3Bytes
  uint8_t  srcId;               //1Byte
  uint8_t  destID;              //1Byte
  uint8_t  routID;              //1Byte
  uint8_t  cmd;                 //1Byte  

}__packed RBFMacHeader_U8_t;

typedef struct {
//  uint8_t *buffer;
//  uint8_t buffer_size;    //TODO:header长度为15Byte;部分协议会精简header长度

  uint32_t rollungCode;    //4Bytes
  uint8_t  frameCtl;       //1Byte


  uint16_t syncClock;      //2Bytes
  uint8_t  hsclock;        //1Byte
  uint32_t panId:24;       //3Bytes
  uint8_t  srcId;          //1Byte
  uint8_t  destID;         //1Byte
  uint8_t  routID;         //1Byte
  uint8_t  cmd;            //1Byte


}__packed RBFMacHeader_t;

/*
 *@note mac数据包格式: 数据包长度(1Bytes)+header(15Bytes)+payload
 *@note 目前数据包长度(RBF_MAC_PACKET_LEN_SIZE)占用1B
 *
 */
#define RBF_MAC_PACKET_LEN_INDEX       0           //MAC层数据包第一个字节为数据的长度
#define RBF_MAC_PACKET_LEN_SIZE        1           //MAC层数据包第一个字节为数据的长度占用一个字节
/*
 *@note header数据包索引
 *
 *
 */
typedef enum{
  RBF_MAC_HEADER_INDEX_FIRST = (RBF_MAC_PACKET_LEN_INDEX+RBF_MAC_PACKET_LEN_SIZE), //TODO: Header index紧跟packet len后面
  RBF_MAC_HEADER_INDEX_ROLLUNGCODE = RBF_MAC_HEADER_INDEX_FIRST, //rollungCode
  RBF_MAC_HEADER_INDEX_FRAMECTL = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+4,
  RBF_MAC_HEADER_INDEX_SYNCCLOCK = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+5,
  RBF_MAC_HEADER_INDEX_HSCLOCK = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+7,
  RBF_MAC_HEADER_INDEX_PANDID = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+8,
  RBF_MAC_HEADER_INDEX_SRCID = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+11,
  RBF_MAC_HEADER_INDEX_DESTID = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+12,
  RBF_MAC_HEADER_INDEX_ROUTID = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+13,
  RBF_MAC_HEADER_INDEX_CMD = RBF_MAC_HEADER_INDEX_ROLLUNGCODE+14,


  RBF_MAC_HEADER_INDEX_LAST = RBF_MAC_HEADER_INDEX_CMD,
}RBFMACHEADER_INDEX_t;

#define RBF_MAC_HEADER_LEN                              sizeof(RBFMacHeader_t) //(RBF_MAC_HEADER_INDEX_LAST-RBF_MAC_HEADER_INDEX_FIRST+1) //
#define RBF_HEADER_LENGTH_MAX                           RBF_MAC_HEADER_LEN     //TODO:是否存在部分协议精简的header
#define RBF_TAIL_LENGTH_MAX                             4
#define RBF_PAYLOAD_LENGTH_MAX                          (255-RBF_HEADER_LENGTH_MAX-RBF_TAIL_LENGTH_MAX-RBF_MAC_PACKET_LEN_SIZE)




#if defined (PACKET_0_INCLUDE)
//Packet[0]  header+payload
#define PACKET_0_OUT_OFFSET                             0


#else
//Packet[0]  packet_len+header +payload
#define PACKET_0_OUT_OFFSET                             RBF_MAC_PACKET_LEN_SIZE


#endif //end of "PACKET_0_INCLUDE"



typedef struct{
  uint8_t rssi;
  uint8_t qrfH1;
  uint8_t qrfH2;
  uint32_t mac;
}__packed RBFMacMessageGatewayAckEvent_t;

typedef enum{
  RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST+1,//15,
  RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_RSSI = RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_FIRST, //1bytes//15
  RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_QRFH1  = RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_FIRST+1, //16
  RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_QRFH2 = RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_FIRST+2, //17

  RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_MAC = RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_FIRST+3,
  RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_LAST = RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_MAC,
}RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_INDEX_t;

#define RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_LEN  sizeof(RBFMacMessageGatewayAckEvent_t)


#if defined (PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_GATEWAY_ACK_EVENT_LEN    (RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX+RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_LEN)
#else
#define RBF_MAC_PACKET_GATEWAY_ACK_EVENT_LEN    (RBF_HEADER_LENGTH_MAX+RBFMAC_PAYLOAD_GATEWAY_ACK_EVENT_LEN)
#endif


#endif /* MAC_RBFMACMESSAGETYPES_H_ */
